/**
 * 
 */
package com.xtsoft.platform.base.system.service;


import com.xtsoft.platform.base.system.domain.SysRole;
import com.xtsoft.platform.base.system.domain.SysUser;
import com.xtsoft.platform.base.system.domain.UserRole;

import com.xtsoft.platform.base.system.repository.SysUserRepository;
import com.xtsoft.platform.base.system.repository.UserRoleRepository;
import com.xtsoft.platform.globe.enums.YesNo;
import com.xtsoft.platform.globe.exception.RolesNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author Administrator
 *
 */
@Service("commonUserDetailsService")
public class CommonUserDetailsService implements UserDetailsService {
    /** 
     * Logger for this class 
     */  
	private static final Logger logger = LoggerFactory.getLogger(CommonUserDetailsService.class);
  
	@Autowired
	private SysUserRepository sysUserRepository;
	
	@Autowired
	private UserRoleRepository userRoleRepository;
	

    /**
     * @Description 载入用户，且载入用户拥有的角色
     * @Param username
     * @return org.springframework.security.core.userdetails.UserDetails
     * @Author defencez
     * @Date 2024-05-15 07:08
    */
    public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException {
            //获得用户
            Optional<SysUser> sysUser = sysUserRepository.findByLoginIdAndState(username, YesNo.Yes.getIntValue());
            if (!sysUser.isPresent()){  
                String message = "账号密码不匹配";
                throw new UsernameNotFoundException(message);
            }  
            
            //获得用户的角色,用于权限验证
            List<UserRole> userRoles = userRoleRepository.findBySysUser(sysUser.get());
			if(null == userRoles || userRoles.isEmpty()){
				String message = "用户没有任何角色";
				throw new RolesNotFoundException(message);
			}

            sysUser.get().getSysRoles().addAll(userRoles.stream().map(action->action.getSysRole()).collect(Collectors.toList()));
            return sysUser.get();
    }
    
	/**
	 * @Description TODO
	 * @Param user
	 * @return java.util.Set<org.springframework.security.core.GrantedAuthority>
	 * @Author defencez
	 * @Date 2024-05-15 07:09
	*/
	private Set<GrantedAuthority> obtionGrantedAuthorities(SysUser user) {
		List<SysRole>  roleList = user.getSysRoles();
		Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
		for(SysRole sysRole : roleList)
			authSet.add(new SimpleGrantedAuthority(sysRole.getAlias()));
		return authSet;
	}


}
